package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Admin;
import com.weipai.springboot.model.AdminLoginRecord;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.AdminService;
import com.weipai.springboot.module.service.FileUploadService;
import com.weipai.springboot.module.util.DateUtils;
import org.omg.CORBA.OBJ_ADAPTER;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/13 09:58
 * <p>
 * 管理员控制类
 */
@RestController
public class AdminController {

    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Autowired
    AdminService adminService;
    @Autowired
    FileUploadService fileUploadService;

    /**
     * 分页查询管理员列表
     *
     * @param page   页码
     * @param size   每页条数
     * @param draw   前台参数，照样返回
     * @param search 搜索，如果无搜索此值为paging
     * @param sort   time，nosort
     * @param way    desc，asc，noway   按创建时间排序
     */
    @GetMapping("/admin/list/{page}/{size}/{draw}/{search}/{sort}/{way}")
    public Response list(@PathVariable("page") Integer page,
                         @PathVariable("size") Integer size,
                         @PathVariable("draw") String draw,
                         @PathVariable("search") String search,
                         @PathVariable("sort") String sort,
                         @PathVariable("way") String way) {
        Response response = new Response();
        PageHelper.startPage(page, size);
        List<Map<String, Object>> list = adminService.selectAllByDel(0, search, sort, way);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        if (list.size() > 0) {
            PageResult<Map<String, Object>> pageList = new PageResult<>(list, page, size, (int) pageInfo.getTotal(), draw);
            response.put("pageList", pageList);
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    /**
     * 管理员的新增
     *
     * @param admin
     * @return
     */
    @PostMapping("/admin/save")
    public Response save(HttpServletRequest request, Admin admin, MultipartFile img) {
        admin.setCreateTime(DateUtils.timeFormat(System.currentTimeMillis()));
        Response response = new Response();
        if (img != null) {
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                admin.setAdminHeadImg(result);
                response = adminService.save(admin);
            }
        } else {
            response = adminService.save(admin);
        }
        return response;
    }

    /**
     * 查询管理员详情
     *
     * @param id:管理员的id
     * @return
     */
    @GetMapping("/admin/getAdminById/{id}")
    public Response getAdminById(@PathVariable("id") String id) {
        return adminService.getAdminById(id);
    }
    
    @GetMapping("admin/getCustomerCategoryList")
    public Response getCustomerCategoryList(){
        return adminService.getCustomerCategoryList();
    }

    /**
     * 
     * @param customerId
     * @param customerCategoryId
     * @param type:1普通会员，2大客户,3技工,4员工
     * @return
     */
    @GetMapping("/admin/changeCustomerCategory/{customerId}/{customerCategoryId}/{type}")
    public Response changeCustomerCategory(@PathVariable("customerId") String customerId,
                                           @PathVariable("customerCategoryId") String customerCategoryId,
                                           @PathVariable("type") String type){
        return adminService.changeCustomerCategory(customerId, customerCategoryId, type);
    }
    

    /**
     * 管理员的修改
     *
     * @param request
     * @param admin
     * @return
     */
    @PostMapping("/admin/update")
    public Response update(HttpServletRequest request, Admin admin, MultipartFile img) {
        Map<String, Object> map = new HashMap<>();
        Response response = new Response();
        if (img != null) {
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                admin.setAdminHeadImg(result);
                response = adminService.update(admin);
            }
        } else {
            response = adminService.update(admin);
        }
        return response;
    }

    /**
     * 删除
     *
     * @param id:账号的id
     * @return
     */
    @DeleteMapping("/admin/deleteById/{id}")
    public Response deleteById(@PathVariable("id") String id) {
        return adminService.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param id:账号的id集合
     * @return
     */
    @DeleteMapping("/admin/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id") String[] id) {
        return adminService.batchDeleteById(id);
    }

    /**
     * 管理员的登录记录---加搜索
     * @param page
     * @param size
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/admin/getAdminLoginRecordList")
    public Response getAdminLoginRecordList(@RequestParam(value = "page", required = false) Integer page,
                                         @RequestParam(value = "size", required = false) Integer size,
                                         @RequestParam(value = "startTime", required = false) String startTime,
                                         @RequestParam(value = "endTime", required = false) String endTime,
                                         @RequestParam(value = "draw", required = false) String draw,
                                         @RequestParam(value = "adminLoginName", required = false) String adminLoginName){
        return adminService.getAdminLoginRecordList(page, size, startTime, endTime, draw, adminLoginName);
    }

}
